package com.love.slidingWindow;

public class PaiLe {

    public static void main(String[] args) {
        checkInclusion("ab","eidbaooo");
    }

    public static boolean checkInclusion(String s1, String s2) {
        {
            int n = s2.length();
            int[] need = new int[26];
            int[] win = new int[26];
            int l = 0, r = 0;
            int valid = 0;
            for (int i = 0; i < s1.length(); i++) {
                char c = s1.charAt(i);
                need[c - 'a']++;
            }
            while (r < n) {
                char c = s2.charAt(r);
                win[c - 'a']++;
                r++;
                if (need[c - 'a'] > 0) {
                    if (win[c - 'a'] <= need[c - 'a']) {
                        valid++;
                    }
                }

                while (r - l >= s1.length()) {
                    char lc = s2.charAt(l);
                    if (valid == s1.length()) {
                        return true;
                    }
                    if (need[lc - 'a'] > 0) {
                        if (win[lc - 'a'] <= need[lc - 'a']) {
                            valid--;
                        }
                    }
                    l++;
                    win[lc - 'a']--;
                }

            }
            return false;
        }
    }
}
